//
//  WBStatusCell.swift
//  微博
//
//  Created by lipengfei on 15/9/8.
//  Copyright © 2015年 lipengfei. All rights reserved.
//

import UIKit

/// 控件之间的间距
let WBStatusCellMargin: CGFloat = 12
///头像的大小
let WBStatusIconWidth: CGFloat = 35
 /// 默认的图片的大小
let WBStatusPictureItemWidth:CGFloat = 90
 /// 默认图片的间距 
let WBStatusPictureItemMargin: CGFloat = 10
 /// 每行的最大图片的数量
let WBStatusPictureMaxCount: CGFloat = 3
 /// 配图视图的最大尺寸 
let WBStatusPictureMaxWidth:CGFloat = WBStatusPictureMaxCount * WBStatusPictureItemWidth + (WBStatusPictureMaxCount - 1) * WBStatusPictureItemMargin


class WBStatusCell: UITableViewCell {
///  微博数据视图模型
    var statusViewModel:WBStatusViewModel?{
        didSet{
            
            topView.statusViewModel = statusViewModel
            print(statusViewModel?.status.pic_urls)
            contentLabel.text = statusViewModel?.status.text
            pictureView.statusViewModel = statusViewModel
            //添加配图的约束
            //随机测试
            pictureViewHeightCons?.constant = pictureView.bounds.height
            pictureViewWidthCons?.constant = pictureView.bounds.width
            pictureViewTopCons?.constant = statusViewModel?.thumbnailURLs?.count == 0 ? 0 : WBStatusCellMargin
        }
    }
    ///图片视图的约束
    ///宽度约束
    var pictureViewWidthCons:NSLayoutConstraint?
    ///高度约束
    var pictureViewHeightCons:NSLayoutConstraint?
    /// 顶部约束
    var pictureViewTopCons:NSLayoutConstraint?
    /// 重写构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 计算行高
    func rowHeight(viewModel:WBStatusViewModel) -> CGFloat{
        //1. 通过为statusViewModel赋值调用didset，获取内容
        statusViewModel = viewModel
        //2. 更新约束
        layoutIfNeeded()
        //3. 底部视图的最大高度 就是cell的高度
        return CGRectGetMaxY(bottomView.frame)
        
    }
    ///布局视图
    func setupUI(){
        // 添加顶部视图
        backgroundColor = UIColor.whiteColor()
        let topSepView = UIView()
        topSepView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        
        contentView.addSubview(topSepView)
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
    
        contentView.addSubview(pictureView)
            contentView.addSubview(bottomView)
        //cell的默认大小事320//44 
        let width = UIScreen.mainScreen().bounds.width
        topSepView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topSepView, size: CGSize(width: width, height: 60))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: WBStatusCellMargin, y: 0))
//       配图 约束
//         let cons =  pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size:CGSize(width: WBStatusPictureMaxWidth, height:WBStatusPictureMaxWidth), offset: CGPoint(x: 0, y: WBStatusPictureItemMargin))
//        
//        pictureViewWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureViewHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureViewTopCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -WBStatusCellMargin, y: WBStatusCellMargin))
    }
///  定义属性
    ///属性懒加载
    //顶部视图
    lazy var topView:WBStatusCellTopView = WBStatusCellTopView()
    // 内容
    lazy var contentLabel:UILabel = UILabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth: UIScreen.mainScreen().bounds.width - 2 * WBStatusCellMargin)
    //底部视图
    lazy var bottomView:WBStatusCellBottmView = WBStatusCellBottmView()
    // 配图
    lazy var pictureView: WBPictureView = WBPictureView()
    
    
    
    
}